{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Market Basket Analysis Introduction\n",
    "\n",
    "Companion notebook to http://pbpython.com/market-basket-analysis.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from mlxtend.frequent_patterns import apriori\n",
    "from mlxtend.frequent_patterns import association_rules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df = pd.read_excel('http://archive.ics.uci.edu/ml/machine-learning-databases/00352/Online%20Retail.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>InvoiceNo</th>\n",
       "      <th>StockCode</th>\n",
       "      <th>Description</th>\n",
       "      <th>Quantity</th>\n",
       "      <th>InvoiceDate</th>\n",
       "      <th>UnitPrice</th>\n",
       "      <th>CustomerID</th>\n",
       "      <th>Country</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>536365</td>\n",
       "      <td>85123A</td>\n",
       "      <td>WHITE HANGING HEART T-LIGHT HOLDER</td>\n",
       "      <td>6</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>2.55</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>536365</td>\n",
       "      <td>71053</td>\n",
       "      <td>WHITE METAL LANTERN</td>\n",
       "      <td>6</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>3.39</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>536365</td>\n",
       "      <td>84406B</td>\n",
       "      <td>CREAM CUPID HEARTS COAT HANGER</td>\n",
       "      <td>8</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>2.75</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>536365</td>\n",
       "      <td>84029G</td>\n",
       "      <td>KNITTED UNION FLAG HOT WATER BOTTLE</td>\n",
       "      <td>6</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>3.39</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>536365</td>\n",
       "      <td>84029E</td>\n",
       "      <td>RED WOOLLY HOTTIE WHITE HEART.</td>\n",
       "      <td>6</td>\n",
       "      <td>2010-12-01 08:26:00</td>\n",
       "      <td>3.39</td>\n",
       "      <td>17850.0</td>\n",
       "      <td>United Kingdom</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  InvoiceNo StockCode                          Description  Quantity  \\\n",
       "0    536365    85123A   WHITE HANGING HEART T-LIGHT HOLDER         6   \n",
       "1    536365     71053                  WHITE METAL LANTERN         6   \n",
       "2    536365    84406B       CREAM CUPID HEARTS COAT HANGER         8   \n",
       "3    536365    84029G  KNITTED UNION FLAG HOT WATER BOTTLE         6   \n",
       "4    536365    84029E       RED WOOLLY HOTTIE WHITE HEART.         6   \n",
       "\n",
       "          InvoiceDate  UnitPrice  CustomerID         Country  \n",
       "0 2010-12-01 08:26:00       2.55     17850.0  United Kingdom  \n",
       "1 2010-12-01 08:26:00       3.39     17850.0  United Kingdom  \n",
       "2 2010-12-01 08:26:00       2.75     17850.0  United Kingdom  \n",
       "3 2010-12-01 08:26:00       3.39     17850.0  United Kingdom  \n",
       "4 2010-12-01 08:26:00       3.39     17850.0  United Kingdom  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Clean up spaces in description and remove any rows that don't have a valid invoice\n",
    "df['Description'] = df['Description'].str.strip()\n",
    "df.dropna(axis=0, subset=['InvoiceNo'], inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df['InvoiceNo'] = df['InvoiceNo'].astype('str')\n",
    "df = df[~df['InvoiceNo'].str.contains('C')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "basket = (df[df['Country'] ==\"France\"]\n",
    "          .groupby(['InvoiceNo', 'Description'])['Quantity']\n",
    "          .sum().unstack().reset_index().fillna(0)\n",
    "          .set_index('InvoiceNo'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Description</th>\n",
       "      <th>10 COLOUR SPACEBOY PEN</th>\n",
       "      <th>12 COLOURED PARTY BALLOONS</th>\n",
       "      <th>12 EGG HOUSE PAINTED WOOD</th>\n",
       "      <th>12 MESSAGE CARDS WITH ENVELOPES</th>\n",
       "      <th>12 PENCIL SMALL TUBE WOODLAND</th>\n",
       "      <th>12 PENCILS SMALL TUBE RED RETROSPOT</th>\n",
       "      <th>12 PENCILS SMALL TUBE SKULL</th>\n",
       "      <th>12 PENCILS TALL TUBE POSY</th>\n",
       "      <th>12 PENCILS TALL TUBE RED RETROSPOT</th>\n",
       "      <th>12 PENCILS TALL TUBE WOODLAND</th>\n",
       "      <th>...</th>\n",
       "      <th>WRAP VINTAGE PETALS  DESIGN</th>\n",
       "      <th>YELLOW COAT RACK PARIS FASHION</th>\n",
       "      <th>YELLOW GIANT GARDEN THERMOMETER</th>\n",
       "      <th>YELLOW SHARK HELICOPTER</th>\n",
       "      <th>ZINC  STAR T-LIGHT HOLDER</th>\n",
       "      <th>ZINC FOLKART SLEIGH BELLS</th>\n",
       "      <th>ZINC HERB GARDEN CONTAINER</th>\n",
       "      <th>ZINC METAL HEART DECORATION</th>\n",
       "      <th>ZINC T-LIGHT HOLDER STAR LARGE</th>\n",
       "      <th>ZINC T-LIGHT HOLDER STARS SMALL</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>InvoiceNo</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>536370</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>536852</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>536974</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>537065</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>537463</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 1563 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "Description  10 COLOUR SPACEBOY PEN  12 COLOURED PARTY BALLOONS  \\\n",
       "InvoiceNo                                                         \n",
       "536370                          0.0                         0.0   \n",
       "536852                          0.0                         0.0   \n",
       "536974                          0.0                         0.0   \n",
       "537065                          0.0                         0.0   \n",
       "537463                          0.0                         0.0   \n",
       "\n",
       "Description  12 EGG HOUSE PAINTED WOOD  12 MESSAGE CARDS WITH ENVELOPES  \\\n",
       "InvoiceNo                                                                 \n",
       "536370                             0.0                              0.0   \n",
       "536852                             0.0                              0.0   \n",
       "536974                             0.0                              0.0   \n",
       "537065                             0.0                              0.0   \n",
       "537463                             0.0                              0.0   \n",
       "\n",
       "Description  12 PENCIL SMALL TUBE WOODLAND  \\\n",
       "InvoiceNo                                    \n",
       "536370                                 0.0   \n",
       "536852                                 0.0   \n",
       "536974                                 0.0   \n",
       "537065                                 0.0   \n",
       "537463                                 0.0   \n",
       "\n",
       "Description  12 PENCILS SMALL TUBE RED RETROSPOT  12 PENCILS SMALL TUBE SKULL  \\\n",
       "InvoiceNo                                                                       \n",
       "536370                                       0.0                          0.0   \n",
       "536852                                       0.0                          0.0   \n",
       "536974                                       0.0                          0.0   \n",
       "537065                                       0.0                          0.0   \n",
       "537463                                       0.0                          0.0   \n",
       "\n",
       "Description  12 PENCILS TALL TUBE POSY  12 PENCILS TALL TUBE RED RETROSPOT  \\\n",
       "InvoiceNo                                                                    \n",
       "536370                             0.0                                 0.0   \n",
       "536852                             0.0                                 0.0   \n",
       "536974                             0.0                                 0.0   \n",
       "537065                             0.0                                 0.0   \n",
       "537463                             0.0                                 0.0   \n",
       "\n",
       "Description  12 PENCILS TALL TUBE WOODLAND               ...                 \\\n",
       "InvoiceNo                                                ...                  \n",
       "536370                                 0.0               ...                  \n",
       "536852                                 0.0               ...                  \n",
       "536974                                 0.0               ...                  \n",
       "537065                                 0.0               ...                  \n",
       "537463                                 0.0               ...                  \n",
       "\n",
       "Description  WRAP VINTAGE PETALS  DESIGN  YELLOW COAT RACK PARIS FASHION  \\\n",
       "InvoiceNo                                                                  \n",
       "536370                               0.0                             0.0   \n",
       "536852                               0.0                             0.0   \n",
       "536974                               0.0                             0.0   \n",
       "537065                               0.0                             0.0   \n",
       "537463                               0.0                             0.0   \n",
       "\n",
       "Description  YELLOW GIANT GARDEN THERMOMETER  YELLOW SHARK HELICOPTER  \\\n",
       "InvoiceNo                                                               \n",
       "536370                                   0.0                      0.0   \n",
       "536852                                   0.0                      0.0   \n",
       "536974                                   0.0                      0.0   \n",
       "537065                                   0.0                      0.0   \n",
       "537463                                   0.0                      0.0   \n",
       "\n",
       "Description  ZINC  STAR T-LIGHT HOLDER  ZINC FOLKART SLEIGH BELLS  \\\n",
       "InvoiceNo                                                           \n",
       "536370                             0.0                        0.0   \n",
       "536852                             0.0                        0.0   \n",
       "536974                             0.0                        0.0   \n",
       "537065                             0.0                        0.0   \n",
       "537463                             0.0                        0.0   \n",
       "\n",
       "Description  ZINC HERB GARDEN CONTAINER  ZINC METAL HEART DECORATION  \\\n",
       "InvoiceNo                                                              \n",
       "536370                              0.0                          0.0   \n",
       "536852                              0.0                          0.0   \n",
       "536974                              0.0                          0.0   \n",
       "537065                              0.0                          0.0   \n",
       "537463                              0.0                          0.0   \n",
       "\n",
       "Description  ZINC T-LIGHT HOLDER STAR LARGE  ZINC T-LIGHT HOLDER STARS SMALL  \n",
       "InvoiceNo                                                                     \n",
       "536370                                  0.0                              0.0  \n",
       "536852                                  0.0                              0.0  \n",
       "536974                                  0.0                              0.0  \n",
       "537065                                  0.0                              0.0  \n",
       "537463                                  0.0                              0.0  \n",
       "\n",
       "[5 rows x 1563 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "basket.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Description</th>\n",
       "      <th>10 COLOUR SPACEBOY PEN</th>\n",
       "      <th>12 COLOURED PARTY BALLOONS</th>\n",
       "      <th>12 EGG HOUSE PAINTED WOOD</th>\n",
       "      <th>12 MESSAGE CARDS WITH ENVELOPES</th>\n",
       "      <th>12 PENCIL SMALL TUBE WOODLAND</th>\n",
       "      <th>12 PENCILS SMALL TUBE RED RETROSPOT</th>\n",
       "      <th>12 PENCILS SMALL TUBE SKULL</th>\n",
       "      <th>12 PENCILS TALL TUBE POSY</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>InvoiceNo</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>536370</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>536852</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>536974</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>537065</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>537463</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Description  10 COLOUR SPACEBOY PEN  12 COLOURED PARTY BALLOONS  \\\n",
       "InvoiceNo                                                         \n",
       "536370                          0.0                         0.0   \n",
       "536852                          0.0                         0.0   \n",
       "536974                          0.0                         0.0   \n",
       "537065                          0.0                         0.0   \n",
       "537463                          0.0                         0.0   \n",
       "\n",
       "Description  12 EGG HOUSE PAINTED WOOD  12 MESSAGE CARDS WITH ENVELOPES  \\\n",
       "InvoiceNo                                                                 \n",
       "536370                             0.0                              0.0   \n",
       "536852                             0.0                              0.0   \n",
       "536974                             0.0                              0.0   \n",
       "537065                             0.0                              0.0   \n",
       "537463                             0.0                              0.0   \n",
       "\n",
       "Description  12 PENCIL SMALL TUBE WOODLAND  \\\n",
       "InvoiceNo                                    \n",
       "536370                                 0.0   \n",
       "536852                                 0.0   \n",
       "536974                                 0.0   \n",
       "537065                                 0.0   \n",
       "537463                                 0.0   \n",
       "\n",
       "Description  12 PENCILS SMALL TUBE RED RETROSPOT  12 PENCILS SMALL TUBE SKULL  \\\n",
       "InvoiceNo                                                                       \n",
       "536370                                       0.0                          0.0   \n",
       "536852                                       0.0                          0.0   \n",
       "536974                                       0.0                          0.0   \n",
       "537065                                       0.0                          0.0   \n",
       "537463                                       0.0                          0.0   \n",
       "\n",
       "Description  12 PENCILS TALL TUBE POSY  \n",
       "InvoiceNo                               \n",
       "536370                             0.0  \n",
       "536852                             0.0  \n",
       "536974                             0.0  \n",
       "537065                             0.0  \n",
       "537463                             0.0  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Show a subset of columns\n",
    "basket.iloc[:,[0,1,2,3,4,5,6, 7]].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Convert the units to 1 hot encoded values\n",
    "def encode_units(x):\n",
    "    if x <= 0:\n",
    "        return 0\n",
    "    if x >= 1:\n",
    "        return 1    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "basket_sets = basket.applymap(encode_units)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# No need to track postage\n",
    "basket_sets.drop('POSTAGE', inplace=True, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Description</th>\n",
       "      <th>10 COLOUR SPACEBOY PEN</th>\n",
       "      <th>12 COLOURED PARTY BALLOONS</th>\n",
       "      <th>12 EGG HOUSE PAINTED WOOD</th>\n",
       "      <th>12 MESSAGE CARDS WITH ENVELOPES</th>\n",
       "      <th>12 PENCIL SMALL TUBE WOODLAND</th>\n",
       "      <th>12 PENCILS SMALL TUBE RED RETROSPOT</th>\n",
       "      <th>12 PENCILS SMALL TUBE SKULL</th>\n",
       "      <th>12 PENCILS TALL TUBE POSY</th>\n",
       "      <th>12 PENCILS TALL TUBE RED RETROSPOT</th>\n",
       "      <th>12 PENCILS TALL TUBE WOODLAND</th>\n",
       "      <th>...</th>\n",
       "      <th>WRAP VINTAGE PETALS  DESIGN</th>\n",
       "      <th>YELLOW COAT RACK PARIS FASHION</th>\n",
       "      <th>YELLOW GIANT GARDEN THERMOMETER</th>\n",
       "      <th>YELLOW SHARK HELICOPTER</th>\n",
       "      <th>ZINC  STAR T-LIGHT HOLDER</th>\n",
       "      <th>ZINC FOLKART SLEIGH BELLS</th>\n",
       "      <th>ZINC HERB GARDEN CONTAINER</th>\n",
       "      <th>ZINC METAL HEART DECORATION</th>\n",
       "      <th>ZINC T-LIGHT HOLDER STAR LARGE</th>\n",
       "      <th>ZINC T-LIGHT HOLDER STARS SMALL</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>InvoiceNo</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>536370</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>536852</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>536974</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>537065</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>537463</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 1562 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "Description  10 COLOUR SPACEBOY PEN  12 COLOURED PARTY BALLOONS  \\\n",
       "InvoiceNo                                                         \n",
       "536370                            0                           0   \n",
       "536852                            0                           0   \n",
       "536974                            0                           0   \n",
       "537065                            0                           0   \n",
       "537463                            0                           0   \n",
       "\n",
       "Description  12 EGG HOUSE PAINTED WOOD  12 MESSAGE CARDS WITH ENVELOPES  \\\n",
       "InvoiceNo                                                                 \n",
       "536370                               0                                0   \n",
       "536852                               0                                0   \n",
       "536974                               0                                0   \n",
       "537065                               0                                0   \n",
       "537463                               0                                0   \n",
       "\n",
       "Description  12 PENCIL SMALL TUBE WOODLAND  \\\n",
       "InvoiceNo                                    \n",
       "536370                                   0   \n",
       "536852                                   0   \n",
       "536974                                   0   \n",
       "537065                                   0   \n",
       "537463                                   0   \n",
       "\n",
       "Description  12 PENCILS SMALL TUBE RED RETROSPOT  12 PENCILS SMALL TUBE SKULL  \\\n",
       "InvoiceNo                                                                       \n",
       "536370                                         0                            0   \n",
       "536852                                         0                            0   \n",
       "536974                                         0                            0   \n",
       "537065                                         0                            0   \n",
       "537463                                         0                            0   \n",
       "\n",
       "Description  12 PENCILS TALL TUBE POSY  12 PENCILS TALL TUBE RED RETROSPOT  \\\n",
       "InvoiceNo                                                                    \n",
       "536370                               0                                   0   \n",
       "536852                               0                                   0   \n",
       "536974                               0                                   0   \n",
       "537065                               0                                   0   \n",
       "537463                               0                                   0   \n",
       "\n",
       "Description  12 PENCILS TALL TUBE WOODLAND               ...                 \\\n",
       "InvoiceNo                                                ...                  \n",
       "536370                                   0               ...                  \n",
       "536852                                   0               ...                  \n",
       "536974                                   0               ...                  \n",
       "537065                                   0               ...                  \n",
       "537463                                   0               ...                  \n",
       "\n",
       "Description  WRAP VINTAGE PETALS  DESIGN  YELLOW COAT RACK PARIS FASHION  \\\n",
       "InvoiceNo                                                                  \n",
       "536370                                 0                               0   \n",
       "536852                                 0                               0   \n",
       "536974                                 0                               0   \n",
       "537065                                 0                               0   \n",
       "537463                                 0                               0   \n",
       "\n",
       "Description  YELLOW GIANT GARDEN THERMOMETER  YELLOW SHARK HELICOPTER  \\\n",
       "InvoiceNo                                                               \n",
       "536370                                     0                        0   \n",
       "536852                                     0                        0   \n",
       "536974                                     0                        0   \n",
       "537065                                     0                        0   \n",
       "537463                                     0                        0   \n",
       "\n",
       "Description  ZINC  STAR T-LIGHT HOLDER  ZINC FOLKART SLEIGH BELLS  \\\n",
       "InvoiceNo                                                           \n",
       "536370                               0                          0   \n",
       "536852                               0                          0   \n",
       "536974                               0                          0   \n",
       "537065                               0                          0   \n",
       "537463                               0                          0   \n",
       "\n",
       "Description  ZINC HERB GARDEN CONTAINER  ZINC METAL HEART DECORATION  \\\n",
       "InvoiceNo                                                              \n",
       "536370                                0                            0   \n",
       "536852                                0                            0   \n",
       "536974                                0                            0   \n",
       "537065                                0                            0   \n",
       "537463                                0                            0   \n",
       "\n",
       "Description  ZINC T-LIGHT HOLDER STAR LARGE  ZINC T-LIGHT HOLDER STARS SMALL  \n",
       "InvoiceNo                                                                     \n",
       "536370                                    0                                0  \n",
       "536852                                    0                                0  \n",
       "536974                                    0                                0  \n",
       "537065                                    0                                0  \n",
       "537463                                    0                                0  \n",
       "\n",
       "[5 rows x 1562 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "basket_sets.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Build up the frequent items\n",
    "frequent_itemsets = apriori(basket_sets, min_support=0.07, use_colnames=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>support</th>\n",
       "      <th>itemsets</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.071429</td>\n",
       "      <td>[4 TRADITIONAL SPINNING TOPS]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.096939</td>\n",
       "      <td>[ALARM CLOCK BAKELIKE GREEN]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.102041</td>\n",
       "      <td>[ALARM CLOCK BAKELIKE PINK]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.094388</td>\n",
       "      <td>[ALARM CLOCK BAKELIKE RED]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.081633</td>\n",
       "      <td>[BAKING SET 9 PIECE RETROSPOT]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    support                        itemsets\n",
       "0  0.071429   [4 TRADITIONAL SPINNING TOPS]\n",
       "1  0.096939    [ALARM CLOCK BAKELIKE GREEN]\n",
       "2  0.102041     [ALARM CLOCK BAKELIKE PINK]\n",
       "3  0.094388      [ALARM CLOCK BAKELIKE RED]\n",
       "4  0.081633  [BAKING SET 9 PIECE RETROSPOT]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frequent_itemsets.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>antecedants</th>\n",
       "      <th>consequents</th>\n",
       "      <th>support</th>\n",
       "      <th>confidence</th>\n",
       "      <th>lift</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>(PLASTERS IN TIN CIRCUS PARADE)</td>\n",
       "      <td>(PLASTERS IN TIN WOODLAND ANIMALS)</td>\n",
       "      <td>0.168367</td>\n",
       "      <td>0.606061</td>\n",
       "      <td>3.545907</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>(PLASTERS IN TIN WOODLAND ANIMALS)</td>\n",
       "      <td>(PLASTERS IN TIN CIRCUS PARADE)</td>\n",
       "      <td>0.170918</td>\n",
       "      <td>0.597015</td>\n",
       "      <td>3.545907</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>(ALARM CLOCK BAKELIKE PINK)</td>\n",
       "      <td>(ALARM CLOCK BAKELIKE GREEN)</td>\n",
       "      <td>0.102041</td>\n",
       "      <td>0.725000</td>\n",
       "      <td>7.478947</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>(ALARM CLOCK BAKELIKE GREEN)</td>\n",
       "      <td>(ALARM CLOCK BAKELIKE PINK)</td>\n",
       "      <td>0.096939</td>\n",
       "      <td>0.763158</td>\n",
       "      <td>7.478947</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>(SET/6 RED SPOTTY PAPER PLATES)</td>\n",
       "      <td>(SET/6 RED SPOTTY PAPER CUPS)</td>\n",
       "      <td>0.127551</td>\n",
       "      <td>0.960000</td>\n",
       "      <td>6.968889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>(SET/6 RED SPOTTY PAPER CUPS)</td>\n",
       "      <td>(SET/6 RED SPOTTY PAPER PLATES)</td>\n",
       "      <td>0.137755</td>\n",
       "      <td>0.888889</td>\n",
       "      <td>6.968889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>(SET/6 RED SPOTTY PAPER CUPS, SET/20 RED RETRO...</td>\n",
       "      <td>(SET/6 RED SPOTTY PAPER PLATES)</td>\n",
       "      <td>0.102041</td>\n",
       "      <td>0.975000</td>\n",
       "      <td>7.644000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>(SET/6 RED SPOTTY PAPER PLATES, SET/6 RED SPOT...</td>\n",
       "      <td>(SET/20 RED RETROSPOT PAPER NAPKINS)</td>\n",
       "      <td>0.122449</td>\n",
       "      <td>0.812500</td>\n",
       "      <td>6.125000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>(SET/6 RED SPOTTY PAPER PLATES, SET/20 RED RET...</td>\n",
       "      <td>(SET/6 RED SPOTTY PAPER CUPS)</td>\n",
       "      <td>0.102041</td>\n",
       "      <td>0.975000</td>\n",
       "      <td>7.077778</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>(SET/6 RED SPOTTY PAPER CUPS)</td>\n",
       "      <td>(SET/6 RED SPOTTY PAPER PLATES, SET/20 RED RET...</td>\n",
       "      <td>0.137755</td>\n",
       "      <td>0.722222</td>\n",
       "      <td>7.077778</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>(SET/20 RED RETROSPOT PAPER NAPKINS)</td>\n",
       "      <td>(SET/6 RED SPOTTY PAPER PLATES, SET/6 RED SPOT...</td>\n",
       "      <td>0.132653</td>\n",
       "      <td>0.750000</td>\n",
       "      <td>6.125000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>(SET/6 RED SPOTTY PAPER PLATES)</td>\n",
       "      <td>(SET/6 RED SPOTTY PAPER CUPS, SET/20 RED RETRO...</td>\n",
       "      <td>0.127551</td>\n",
       "      <td>0.780000</td>\n",
       "      <td>7.644000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>(PLASTERS IN TIN CIRCUS PARADE)</td>\n",
       "      <td>(PLASTERS IN TIN SPACEBOY)</td>\n",
       "      <td>0.168367</td>\n",
       "      <td>0.530303</td>\n",
       "      <td>3.849607</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>(PLASTERS IN TIN SPACEBOY)</td>\n",
       "      <td>(PLASTERS IN TIN CIRCUS PARADE)</td>\n",
       "      <td>0.137755</td>\n",
       "      <td>0.648148</td>\n",
       "      <td>3.849607</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>(PLASTERS IN TIN WOODLAND ANIMALS)</td>\n",
       "      <td>(PLASTERS IN TIN SPACEBOY)</td>\n",
       "      <td>0.170918</td>\n",
       "      <td>0.611940</td>\n",
       "      <td>4.442233</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>(PLASTERS IN TIN SPACEBOY)</td>\n",
       "      <td>(PLASTERS IN TIN WOODLAND ANIMALS)</td>\n",
       "      <td>0.137755</td>\n",
       "      <td>0.759259</td>\n",
       "      <td>4.442233</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>(ALARM CLOCK BAKELIKE PINK)</td>\n",
       "      <td>(ALARM CLOCK BAKELIKE RED)</td>\n",
       "      <td>0.102041</td>\n",
       "      <td>0.725000</td>\n",
       "      <td>7.681081</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>(ALARM CLOCK BAKELIKE RED)</td>\n",
       "      <td>(ALARM CLOCK BAKELIKE PINK)</td>\n",
       "      <td>0.094388</td>\n",
       "      <td>0.783784</td>\n",
       "      <td>7.681081</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>(ALARM CLOCK BAKELIKE GREEN)</td>\n",
       "      <td>(ALARM CLOCK BAKELIKE RED)</td>\n",
       "      <td>0.096939</td>\n",
       "      <td>0.815789</td>\n",
       "      <td>8.642959</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>(ALARM CLOCK BAKELIKE RED)</td>\n",
       "      <td>(ALARM CLOCK BAKELIKE GREEN)</td>\n",
       "      <td>0.094388</td>\n",
       "      <td>0.837838</td>\n",
       "      <td>8.642959</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>(SET/6 RED SPOTTY PAPER CUPS)</td>\n",
       "      <td>(SET/20 RED RETROSPOT PAPER NAPKINS)</td>\n",
       "      <td>0.137755</td>\n",
       "      <td>0.740741</td>\n",
       "      <td>5.584046</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>(SET/20 RED RETROSPOT PAPER NAPKINS)</td>\n",
       "      <td>(SET/6 RED SPOTTY PAPER CUPS)</td>\n",
       "      <td>0.132653</td>\n",
       "      <td>0.769231</td>\n",
       "      <td>5.584046</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>(SPACEBOY LUNCH BOX)</td>\n",
       "      <td>(DOLLY GIRL LUNCH BOX)</td>\n",
       "      <td>0.125000</td>\n",
       "      <td>0.571429</td>\n",
       "      <td>5.743590</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>(DOLLY GIRL LUNCH BOX)</td>\n",
       "      <td>(SPACEBOY LUNCH BOX)</td>\n",
       "      <td>0.099490</td>\n",
       "      <td>0.717949</td>\n",
       "      <td>5.743590</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>(SET/6 RED SPOTTY PAPER PLATES)</td>\n",
       "      <td>(SET/20 RED RETROSPOT PAPER NAPKINS)</td>\n",
       "      <td>0.127551</td>\n",
       "      <td>0.800000</td>\n",
       "      <td>6.030769</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>(SET/20 RED RETROSPOT PAPER NAPKINS)</td>\n",
       "      <td>(SET/6 RED SPOTTY PAPER PLATES)</td>\n",
       "      <td>0.132653</td>\n",
       "      <td>0.769231</td>\n",
       "      <td>6.030769</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                          antecedants  \\\n",
       "0                     (PLASTERS IN TIN CIRCUS PARADE)   \n",
       "1                  (PLASTERS IN TIN WOODLAND ANIMALS)   \n",
       "2                         (ALARM CLOCK BAKELIKE PINK)   \n",
       "3                        (ALARM CLOCK BAKELIKE GREEN)   \n",
       "4                     (SET/6 RED SPOTTY PAPER PLATES)   \n",
       "5                       (SET/6 RED SPOTTY PAPER CUPS)   \n",
       "6   (SET/6 RED SPOTTY PAPER CUPS, SET/20 RED RETRO...   \n",
       "7   (SET/6 RED SPOTTY PAPER PLATES, SET/6 RED SPOT...   \n",
       "8   (SET/6 RED SPOTTY PAPER PLATES, SET/20 RED RET...   \n",
       "9                       (SET/6 RED SPOTTY PAPER CUPS)   \n",
       "10               (SET/20 RED RETROSPOT PAPER NAPKINS)   \n",
       "11                    (SET/6 RED SPOTTY PAPER PLATES)   \n",
       "12                    (PLASTERS IN TIN CIRCUS PARADE)   \n",
       "13                         (PLASTERS IN TIN SPACEBOY)   \n",
       "14                 (PLASTERS IN TIN WOODLAND ANIMALS)   \n",
       "15                         (PLASTERS IN TIN SPACEBOY)   \n",
       "16                        (ALARM CLOCK BAKELIKE PINK)   \n",
       "17                         (ALARM CLOCK BAKELIKE RED)   \n",
       "18                       (ALARM CLOCK BAKELIKE GREEN)   \n",
       "19                         (ALARM CLOCK BAKELIKE RED)   \n",
       "20                      (SET/6 RED SPOTTY PAPER CUPS)   \n",
       "21               (SET/20 RED RETROSPOT PAPER NAPKINS)   \n",
       "22                               (SPACEBOY LUNCH BOX)   \n",
       "23                             (DOLLY GIRL LUNCH BOX)   \n",
       "24                    (SET/6 RED SPOTTY PAPER PLATES)   \n",
       "25               (SET/20 RED RETROSPOT PAPER NAPKINS)   \n",
       "\n",
       "                                          consequents   support  confidence  \\\n",
       "0                  (PLASTERS IN TIN WOODLAND ANIMALS)  0.168367    0.606061   \n",
       "1                     (PLASTERS IN TIN CIRCUS PARADE)  0.170918    0.597015   \n",
       "2                        (ALARM CLOCK BAKELIKE GREEN)  0.102041    0.725000   \n",
       "3                         (ALARM CLOCK BAKELIKE PINK)  0.096939    0.763158   \n",
       "4                       (SET/6 RED SPOTTY PAPER CUPS)  0.127551    0.960000   \n",
       "5                     (SET/6 RED SPOTTY PAPER PLATES)  0.137755    0.888889   \n",
       "6                     (SET/6 RED SPOTTY PAPER PLATES)  0.102041    0.975000   \n",
       "7                (SET/20 RED RETROSPOT PAPER NAPKINS)  0.122449    0.812500   \n",
       "8                       (SET/6 RED SPOTTY PAPER CUPS)  0.102041    0.975000   \n",
       "9   (SET/6 RED SPOTTY PAPER PLATES, SET/20 RED RET...  0.137755    0.722222   \n",
       "10  (SET/6 RED SPOTTY PAPER PLATES, SET/6 RED SPOT...  0.132653    0.750000   \n",
       "11  (SET/6 RED SPOTTY PAPER CUPS, SET/20 RED RETRO...  0.127551    0.780000   \n",
       "12                         (PLASTERS IN TIN SPACEBOY)  0.168367    0.530303   \n",
       "13                    (PLASTERS IN TIN CIRCUS PARADE)  0.137755    0.648148   \n",
       "14                         (PLASTERS IN TIN SPACEBOY)  0.170918    0.611940   \n",
       "15                 (PLASTERS IN TIN WOODLAND ANIMALS)  0.137755    0.759259   \n",
       "16                         (ALARM CLOCK BAKELIKE RED)  0.102041    0.725000   \n",
       "17                        (ALARM CLOCK BAKELIKE PINK)  0.094388    0.783784   \n",
       "18                         (ALARM CLOCK BAKELIKE RED)  0.096939    0.815789   \n",
       "19                       (ALARM CLOCK BAKELIKE GREEN)  0.094388    0.837838   \n",
       "20               (SET/20 RED RETROSPOT PAPER NAPKINS)  0.137755    0.740741   \n",
       "21                      (SET/6 RED SPOTTY PAPER CUPS)  0.132653    0.769231   \n",
       "22                             (DOLLY GIRL LUNCH BOX)  0.125000    0.571429   \n",
       "23                               (SPACEBOY LUNCH BOX)  0.099490    0.717949   \n",
       "24               (SET/20 RED RETROSPOT PAPER NAPKINS)  0.127551    0.800000   \n",
       "25                    (SET/6 RED SPOTTY PAPER PLATES)  0.132653    0.769231   \n",
       "\n",
       "        lift  \n",
       "0   3.545907  \n",
       "1   3.545907  \n",
       "2   7.478947  \n",
       "3   7.478947  \n",
       "4   6.968889  \n",
       "5   6.968889  \n",
       "6   7.644000  \n",
       "7   6.125000  \n",
       "8   7.077778  \n",
       "9   7.077778  \n",
       "10  6.125000  \n",
       "11  7.644000  \n",
       "12  3.849607  \n",
       "13  3.849607  \n",
       "14  4.442233  \n",
       "15  4.442233  \n",
       "16  7.681081  \n",
       "17  7.681081  \n",
       "18  8.642959  \n",
       "19  8.642959  \n",
       "20  5.584046  \n",
       "21  5.584046  \n",
       "22  5.743590  \n",
       "23  5.743590  \n",
       "24  6.030769  \n",
       "25  6.030769  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create the rules\n",
    "rules = association_rules(frequent_itemsets, metric=\"lift\", min_threshold=1)\n",
    "rules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>antecedants</th>\n",
       "      <th>consequents</th>\n",
       "      <th>support</th>\n",
       "      <th>confidence</th>\n",
       "      <th>lift</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>(SET/6 RED SPOTTY PAPER PLATES)</td>\n",
       "      <td>(SET/6 RED SPOTTY PAPER CUPS)</td>\n",
       "      <td>0.127551</td>\n",
       "      <td>0.960000</td>\n",
       "      <td>6.968889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>(SET/6 RED SPOTTY PAPER CUPS)</td>\n",
       "      <td>(SET/6 RED SPOTTY PAPER PLATES)</td>\n",
       "      <td>0.137755</td>\n",
       "      <td>0.888889</td>\n",
       "      <td>6.968889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>(SET/6 RED SPOTTY PAPER CUPS, SET/20 RED RETRO...</td>\n",
       "      <td>(SET/6 RED SPOTTY PAPER PLATES)</td>\n",
       "      <td>0.102041</td>\n",
       "      <td>0.975000</td>\n",
       "      <td>7.644000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>(SET/6 RED SPOTTY PAPER PLATES, SET/6 RED SPOT...</td>\n",
       "      <td>(SET/20 RED RETROSPOT PAPER NAPKINS)</td>\n",
       "      <td>0.122449</td>\n",
       "      <td>0.812500</td>\n",
       "      <td>6.125000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>(SET/6 RED SPOTTY PAPER PLATES, SET/20 RED RET...</td>\n",
       "      <td>(SET/6 RED SPOTTY PAPER CUPS)</td>\n",
       "      <td>0.102041</td>\n",
       "      <td>0.975000</td>\n",
       "      <td>7.077778</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>(ALARM CLOCK BAKELIKE GREEN)</td>\n",
       "      <td>(ALARM CLOCK BAKELIKE RED)</td>\n",
       "      <td>0.096939</td>\n",
       "      <td>0.815789</td>\n",
       "      <td>8.642959</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>(ALARM CLOCK BAKELIKE RED)</td>\n",
       "      <td>(ALARM CLOCK BAKELIKE GREEN)</td>\n",
       "      <td>0.094388</td>\n",
       "      <td>0.837838</td>\n",
       "      <td>8.642959</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>(SET/6 RED SPOTTY PAPER PLATES)</td>\n",
       "      <td>(SET/20 RED RETROSPOT PAPER NAPKINS)</td>\n",
       "      <td>0.127551</td>\n",
       "      <td>0.800000</td>\n",
       "      <td>6.030769</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                          antecedants  \\\n",
       "4                     (SET/6 RED SPOTTY PAPER PLATES)   \n",
       "5                       (SET/6 RED SPOTTY PAPER CUPS)   \n",
       "6   (SET/6 RED SPOTTY PAPER CUPS, SET/20 RED RETRO...   \n",
       "7   (SET/6 RED SPOTTY PAPER PLATES, SET/6 RED SPOT...   \n",
       "8   (SET/6 RED SPOTTY PAPER PLATES, SET/20 RED RET...   \n",
       "18                       (ALARM CLOCK BAKELIKE GREEN)   \n",
       "19                         (ALARM CLOCK BAKELIKE RED)   \n",
       "24                    (SET/6 RED SPOTTY PAPER PLATES)   \n",
       "\n",
       "                             consequents   support  confidence      lift  \n",
       "4          (SET/6 RED SPOTTY PAPER CUPS)  0.127551    0.960000  6.968889  \n",
       "5        (SET/6 RED SPOTTY PAPER PLATES)  0.137755    0.888889  6.968889  \n",
       "6        (SET/6 RED SPOTTY PAPER PLATES)  0.102041    0.975000  7.644000  \n",
       "7   (SET/20 RED RETROSPOT PAPER NAPKINS)  0.122449    0.812500  6.125000  \n",
       "8          (SET/6 RED SPOTTY PAPER CUPS)  0.102041    0.975000  7.077778  \n",
       "18            (ALARM CLOCK BAKELIKE RED)  0.096939    0.815789  8.642959  \n",
       "19          (ALARM CLOCK BAKELIKE GREEN)  0.094388    0.837838  8.642959  \n",
       "24  (SET/20 RED RETROSPOT PAPER NAPKINS)  0.127551    0.800000  6.030769  "
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rules[ (rules['lift'] >= 6) &\n",
    "       (rules['confidence'] >= 0.8) ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "340.0"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "basket['ALARM CLOCK BAKELIKE GREEN'].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "316.0"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "basket['ALARM CLOCK BAKELIKE RED'].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "basket2 = (df[df['Country'] ==\"Germany\"]\n",
    "          .groupby(['InvoiceNo', 'Description'])['Quantity']\n",
    "          .sum().unstack().reset_index().fillna(0)\n",
    "          .set_index('InvoiceNo'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "basket_sets2 = basket2.applymap(encode_units)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "basket_sets2.drop('POSTAGE', inplace=True, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "frequent_itemsets2 = apriori(basket_sets2, min_support=0.05, use_colnames=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>antecedants</th>\n",
       "      <th>consequents</th>\n",
       "      <th>support</th>\n",
       "      <th>confidence</th>\n",
       "      <th>lift</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>(WOODLAND CHARLOTTE BAG)</td>\n",
       "      <td>(ROUND SNACK BOXES SET OF4 WOODLAND)</td>\n",
       "      <td>0.126915</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>2.040179</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>(ROUND SNACK BOXES SET OF4 WOODLAND)</td>\n",
       "      <td>(WOODLAND CHARLOTTE BAG)</td>\n",
       "      <td>0.245077</td>\n",
       "      <td>0.258929</td>\n",
       "      <td>2.040179</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>(PLASTERS IN TIN CIRCUS PARADE)</td>\n",
       "      <td>(ROUND SNACK BOXES SET OF4 WOODLAND)</td>\n",
       "      <td>0.115974</td>\n",
       "      <td>0.490566</td>\n",
       "      <td>2.001685</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>(ROUND SNACK BOXES SET OF4 WOODLAND)</td>\n",
       "      <td>(PLASTERS IN TIN CIRCUS PARADE)</td>\n",
       "      <td>0.245077</td>\n",
       "      <td>0.232143</td>\n",
       "      <td>2.001685</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>(PLASTERS IN TIN WOODLAND ANIMALS)</td>\n",
       "      <td>(PLASTERS IN TIN SPACEBOY)</td>\n",
       "      <td>0.137856</td>\n",
       "      <td>0.444444</td>\n",
       "      <td>4.145125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>(PLASTERS IN TIN SPACEBOY)</td>\n",
       "      <td>(PLASTERS IN TIN WOODLAND ANIMALS)</td>\n",
       "      <td>0.107221</td>\n",
       "      <td>0.571429</td>\n",
       "      <td>4.145125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>(WOODLAND CHARLOTTE BAG)</td>\n",
       "      <td>(RED RETROSPOT CHARLOTTE BAG)</td>\n",
       "      <td>0.126915</td>\n",
       "      <td>0.465517</td>\n",
       "      <td>6.648168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>(RED RETROSPOT CHARLOTTE BAG)</td>\n",
       "      <td>(WOODLAND CHARLOTTE BAG)</td>\n",
       "      <td>0.070022</td>\n",
       "      <td>0.843750</td>\n",
       "      <td>6.648168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>(ROUND SNACK BOXES SET OF4 WOODLAND)</td>\n",
       "      <td>(ROUND SNACK BOXES SET OF 4 FRUITS)</td>\n",
       "      <td>0.245077</td>\n",
       "      <td>0.535714</td>\n",
       "      <td>3.400298</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>(ROUND SNACK BOXES SET OF 4 FRUITS)</td>\n",
       "      <td>(ROUND SNACK BOXES SET OF4 WOODLAND)</td>\n",
       "      <td>0.157549</td>\n",
       "      <td>0.833333</td>\n",
       "      <td>3.400298</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>(PLASTERS IN TIN WOODLAND ANIMALS)</td>\n",
       "      <td>(ROUND SNACK BOXES SET OF4 WOODLAND)</td>\n",
       "      <td>0.137856</td>\n",
       "      <td>0.539683</td>\n",
       "      <td>2.202098</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>(ROUND SNACK BOXES SET OF4 WOODLAND)</td>\n",
       "      <td>(PLASTERS IN TIN WOODLAND ANIMALS)</td>\n",
       "      <td>0.245077</td>\n",
       "      <td>0.303571</td>\n",
       "      <td>2.202098</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>(PLASTERS IN TIN CIRCUS PARADE)</td>\n",
       "      <td>(PLASTERS IN TIN WOODLAND ANIMALS)</td>\n",
       "      <td>0.115974</td>\n",
       "      <td>0.584906</td>\n",
       "      <td>4.242887</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>(PLASTERS IN TIN WOODLAND ANIMALS)</td>\n",
       "      <td>(PLASTERS IN TIN CIRCUS PARADE)</td>\n",
       "      <td>0.137856</td>\n",
       "      <td>0.492063</td>\n",
       "      <td>4.242887</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>(PLASTERS IN TIN CIRCUS PARADE)</td>\n",
       "      <td>(ROUND SNACK BOXES SET OF 4 FRUITS)</td>\n",
       "      <td>0.115974</td>\n",
       "      <td>0.433962</td>\n",
       "      <td>2.754455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>(ROUND SNACK BOXES SET OF 4 FRUITS)</td>\n",
       "      <td>(PLASTERS IN TIN CIRCUS PARADE)</td>\n",
       "      <td>0.157549</td>\n",
       "      <td>0.319444</td>\n",
       "      <td>2.754455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>(SPACEBOY LUNCH BOX)</td>\n",
       "      <td>(ROUND SNACK BOXES SET OF4 WOODLAND)</td>\n",
       "      <td>0.102845</td>\n",
       "      <td>0.680851</td>\n",
       "      <td>2.778116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>(ROUND SNACK BOXES SET OF4 WOODLAND)</td>\n",
       "      <td>(SPACEBOY LUNCH BOX)</td>\n",
       "      <td>0.245077</td>\n",
       "      <td>0.285714</td>\n",
       "      <td>2.778116</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             antecedants  \\\n",
       "0               (WOODLAND CHARLOTTE BAG)   \n",
       "1   (ROUND SNACK BOXES SET OF4 WOODLAND)   \n",
       "2        (PLASTERS IN TIN CIRCUS PARADE)   \n",
       "3   (ROUND SNACK BOXES SET OF4 WOODLAND)   \n",
       "4     (PLASTERS IN TIN WOODLAND ANIMALS)   \n",
       "5             (PLASTERS IN TIN SPACEBOY)   \n",
       "6               (WOODLAND CHARLOTTE BAG)   \n",
       "7          (RED RETROSPOT CHARLOTTE BAG)   \n",
       "8   (ROUND SNACK BOXES SET OF4 WOODLAND)   \n",
       "9    (ROUND SNACK BOXES SET OF 4 FRUITS)   \n",
       "10    (PLASTERS IN TIN WOODLAND ANIMALS)   \n",
       "11  (ROUND SNACK BOXES SET OF4 WOODLAND)   \n",
       "12       (PLASTERS IN TIN CIRCUS PARADE)   \n",
       "13    (PLASTERS IN TIN WOODLAND ANIMALS)   \n",
       "14       (PLASTERS IN TIN CIRCUS PARADE)   \n",
       "15   (ROUND SNACK BOXES SET OF 4 FRUITS)   \n",
       "16                  (SPACEBOY LUNCH BOX)   \n",
       "17  (ROUND SNACK BOXES SET OF4 WOODLAND)   \n",
       "\n",
       "                             consequents   support  confidence      lift  \n",
       "0   (ROUND SNACK BOXES SET OF4 WOODLAND)  0.126915    0.500000  2.040179  \n",
       "1               (WOODLAND CHARLOTTE BAG)  0.245077    0.258929  2.040179  \n",
       "2   (ROUND SNACK BOXES SET OF4 WOODLAND)  0.115974    0.490566  2.001685  \n",
       "3        (PLASTERS IN TIN CIRCUS PARADE)  0.245077    0.232143  2.001685  \n",
       "4             (PLASTERS IN TIN SPACEBOY)  0.137856    0.444444  4.145125  \n",
       "5     (PLASTERS IN TIN WOODLAND ANIMALS)  0.107221    0.571429  4.145125  \n",
       "6          (RED RETROSPOT CHARLOTTE BAG)  0.126915    0.465517  6.648168  \n",
       "7               (WOODLAND CHARLOTTE BAG)  0.070022    0.843750  6.648168  \n",
       "8    (ROUND SNACK BOXES SET OF 4 FRUITS)  0.245077    0.535714  3.400298  \n",
       "9   (ROUND SNACK BOXES SET OF4 WOODLAND)  0.157549    0.833333  3.400298  \n",
       "10  (ROUND SNACK BOXES SET OF4 WOODLAND)  0.137856    0.539683  2.202098  \n",
       "11    (PLASTERS IN TIN WOODLAND ANIMALS)  0.245077    0.303571  2.202098  \n",
       "12    (PLASTERS IN TIN WOODLAND ANIMALS)  0.115974    0.584906  4.242887  \n",
       "13       (PLASTERS IN TIN CIRCUS PARADE)  0.137856    0.492063  4.242887  \n",
       "14   (ROUND SNACK BOXES SET OF 4 FRUITS)  0.115974    0.433962  2.754455  \n",
       "15       (PLASTERS IN TIN CIRCUS PARADE)  0.157549    0.319444  2.754455  \n",
       "16  (ROUND SNACK BOXES SET OF4 WOODLAND)  0.102845    0.680851  2.778116  \n",
       "17                  (SPACEBOY LUNCH BOX)  0.245077    0.285714  2.778116  "
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rules2 = association_rules(frequent_itemsets2, metric=\"lift\", min_threshold=1)\n",
    "rules2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>antecedants</th>\n",
       "      <th>consequents</th>\n",
       "      <th>support</th>\n",
       "      <th>confidence</th>\n",
       "      <th>lift</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>(PLASTERS IN TIN SPACEBOY)</td>\n",
       "      <td>(PLASTERS IN TIN WOODLAND ANIMALS)</td>\n",
       "      <td>0.107221</td>\n",
       "      <td>0.571429</td>\n",
       "      <td>4.145125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>(RED RETROSPOT CHARLOTTE BAG)</td>\n",
       "      <td>(WOODLAND CHARLOTTE BAG)</td>\n",
       "      <td>0.070022</td>\n",
       "      <td>0.843750</td>\n",
       "      <td>6.648168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>(PLASTERS IN TIN CIRCUS PARADE)</td>\n",
       "      <td>(PLASTERS IN TIN WOODLAND ANIMALS)</td>\n",
       "      <td>0.115974</td>\n",
       "      <td>0.584906</td>\n",
       "      <td>4.242887</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        antecedants                         consequents  \\\n",
       "5        (PLASTERS IN TIN SPACEBOY)  (PLASTERS IN TIN WOODLAND ANIMALS)   \n",
       "7     (RED RETROSPOT CHARLOTTE BAG)            (WOODLAND CHARLOTTE BAG)   \n",
       "12  (PLASTERS IN TIN CIRCUS PARADE)  (PLASTERS IN TIN WOODLAND ANIMALS)   \n",
       "\n",
       "     support  confidence      lift  \n",
       "5   0.107221    0.571429  4.145125  \n",
       "7   0.070022    0.843750  6.648168  \n",
       "12  0.115974    0.584906  4.242887  "
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rules2[ (rules2['lift'] >= 4) &\n",
    "        (rules2['confidence'] >= 0.5) ]"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
